#!/bin/sh
#
#


BASE_SYS_ROOT=${SYSROOT_PATH}
PREGEN_PATH=${MWL_TOP}/linux/pregen


if [ x${MWL_USE_MDEV} != x"1" ]; then
	export MWP_UDEV=udev-116
fi


cd ${BUILD_PATH}
for pkg in ${MWP_BUSYBOX} ${MWP_UDEV}; do
	if [ -f ${SRC_PATH}/${pkg}.tar.bz2 ]; then
		echo -n "Extracting ${pkg} ... "
		tar jxf ${SRC_PATH}/${pkg}.tar.bz2
		echo "OK."
	else
		echo "No ${pkg} source package found!"
		exit
	fi
done
echo


mkdir -p ${BASE_SYS_ROOT}
cd ${BASE_SYS_ROOT}

for dir in lib usr/lib bin usr/bin sbin usr/sbin sys dev etc/init.d proc root home var tmp; do
	mkdir -p ${dir}
done


if [ ${BASE_SYS_ROOT} != ${SYSROOT_PATH} ]; then
#	cp -a ${SYSROOT_PATH}/lib/*.so* lib/
#	If uClibc ???
	for LIBS in ld-*.so* \
				libanl*.so* \
				libBrokenLocale*.so* \
				libc-*.so \
				libc.so.* \
				libcrypt*.so* \
				libdl*.so* \
				libm*.so* \
				libmemusage.so \
				libnsl*.so* \
				libnss_compat*.so* \
				libnss_dns*.so* \
				libnss_files*.so* \
				libnss_hesiod*.so* \
				libnss_nis*.so* \
				libpcprofile.so \
				libpthrea*.so* \
				libresolv*.so* \
				librt*.so* \
				libSegFault.so \
				libthread_db*.so* \
				libutil*.so*
	do
		cp -a ${SYSROOT_PATH}/lib/${LIBS} lib/
	done

#	cp -a ${SYSROOT_PATH}/usr/lib/*.so* usr/lib/
#	If uClibc ???
	for LIBS in libanl.so \
				libBrokenLocale.so \
				libcrypt.so \
				libc.so \
				libdl.so \
				libm.so \
				libnsl.so \
				libnss_compat.so \
				libnss_dns.so \
				libnss_files.so \
				libnss_hesiod.so \
				libnss_nisplus.so \
				libnss_nis.so \
				libpthread.so \
				libresolv.so \
				librt.so \
				libthread_db.so \
				libutil.so
	do
		cp -a ${SYSROOT_PATH}/usr/lib/${LIBS} usr/lib/
	done
fi

# fixme: any better solution? 
#cp -a ${SYSROOT_PATH}/usr/${TARGET_PLAT}/lib/lib*.so* usr/lib/
#	If uClibc ???
for LIBS in libgcc_s.so* \
			libgomp.so* \
			libmudflap.so* \
			libmudflapth.so* \
			libssp.so* \
			libstdc++.so*
do
	cp -a ${SYSROOT_PATH}/usr/${TARGET_PLAT}/lib/${LIBS} usr/lib/
done


if [ ! -c dev/console ]; then
	fakeroot mknod -m 666 dev/console c 5 1
fi


cd ${BUILD_PATH}/${MWP_BUSYBOX}
echo -n "Configuring ${MWP_BUSYBOX} ."

sed -i 's/^\(EXTRAVERSION.*=\).*/\1 -noon/' Makefile
sed -i 's/^\(ARCH.*=\).*/\1 arm/' Makefile
sed -i "s/^\(CROSS_COMPILE.*=\).*/\1 ${TARGET_PLAT}-/" Makefile

make defconfig > /dev/null 2>&1
echo -n "."

if [ x${MWL_USE_MDEV} != x"1" ]; then
	sed -i 's/^\(CONFIG.*_MDEV_.*\)=y/# \1 is not set/g' .config
fi
sed -i 's/^\(CONFIG.*SWAP.*\)=y/# \1 is not set/g' .config
sed -i 's/^# \(CONFIG.*RPC\) is not set/\1=y/g' .config
sed -i 's/^# \(CONFIG.*FANCY_PROMPT\) is not set/\1=y/g' .config
sed -i "s:CONFIG_PREFIX.*:CONFIG_PREFIX=\"${BASE_SYS_ROOT}\":" .config

sed -i 's/\(message.*\)init started.*/\1\\nBusybox v" BB_VER " for MaxWit Linux\\n");/' init/init.c
# sed -i 's/\(message.*\)init started\: %s\(.*\)/\1\\n%s\\ninit started\2/' init/init.c
sed -i '/new_init_action.*VC_/d' init/init.c
sed -i 's/^\(static.*log_console =\).*/\1 NULL;/' init/init.c
sed -i '/Log the process/,+3d' init/init.c
sed -i '/NB.*wait for/,+24d' init/init.c

sed -i 's:\(^#define BANNER.*BB_VER\).*:\1 \" for MaxWit Linux (http\://www.maxwit.com)\":' libbb/messages.c
sed -i 's/\(.*\) ALIGN1 =/ALIGN1 \1 =/' libbb/messages.c

sed -i 's/\(text.*=.*PS1=.*\)w /\1w/' shell/ash.c
sed -i 's/\(text.*=.*PS1=\)/\1maxwit:/' shell/ash.c

for file in init/init.c shell/ash.c; do
	sed -i 's/\(.*\) MAIN_EXTERNALLY_VISIBLE;/MAIN_EXTERNALLY_VISIBLE \1;/' ${file}
	sed -i 's/\(^[a-zA-Z].*\) ALIGN1 =/ALIGN1 \1 =/' ${file}
	sed -i 's/\<iflag\>/IFLAG/g' ${file}
	sed -i 's/\<ifsval\>/IFSVAL/g' ${file}
	sed -i 's/\<ifsset>/IFSSET/g' ${file}
done

sed -i 's`\(#include <\)linux`\1sys`' miscutils/inotifyd.c

make oldconfig > /dev/null 2>&1 

echo ". OK."


make -j4 V=1 && \
make install || exit 2


cp ${PREGEN_PATH}/rcS ${BASE_SYS_ROOT}/etc/init.d/
chmod 777 ${BASE_SYS_ROOT}/etc/init.d/rcS


if [ x${MWL_USE_MDEV} != x"1" ]; then
	echo "Building udev ..."
	cd ${BUILD_PATH}/${MWP_UDEV}

## sed -i '/open.*null.*O_/i\\tmknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));' udevd.c
	sed -i '/in,out,err/,+12d' udevd.c

	make CROSS_COMPILE=${TARGET_PLAT}- V=1 && \
	make DESTDIR=${BASE_SYS_ROOT} install-bin || exit

	mkdir -p ${BASE_SYS_ROOT}/etc/udev/rules.d
	cp ${PREGEN_PATH}/20-names.rules ${BASE_SYS_ROOT}/etc/udev/rules.d/

	sed -i '/^mdev/d' ${BASE_SYS_ROOT}/etc/init.d/rcS
else
	echo -n "Installing mdev ... "
	sed -i '/^udev/d' ${BASE_SYS_ROOT}/etc/init.d/rcS
	sed -i 's/udev/mdev/g' ${BASE_SYS_ROOT}/etc/init.d/rcS
	cp ${PREGEN_PATH}/mdev.conf ${BASE_SYS_ROOT}/etc/
	cp -a ${PREGEN_PATH}/mdev ${BASE_SYS_ROOT}/lib/
fi


echo "Done."

